# coding: utf-8
from sqlalchemy import BigInteger, Column, DateTime, String, text
from sqlalchemy.dialects.mysql import TINYINT
from sqlalchemy.ext.declarative import declarative_base
from datetime import datetime
from pydantic import BaseModel, field_validator

Base = declarative_base()
metadata = Base.metadata # type: ignore


class UserPersonal(Base):
    __tablename__ = 'user_personal'
    __table_args__ = {'comment': '用户个人信息表'}

    userPersonalId = Column(String(256), primary_key=True, comment='id')
    userAccountId = Column(String(256), nullable=False, comment='账号 id')
    userName = Column(String(256), nullable=False, comment='员工姓名')
    userGender = Column(TINYINT, server_default=text("'0'"), comment='员工性别')
    userIdCardNum = Column(String(36), nullable=False, comment='员工身份证号')
    userPhoneNumber = Column(String(22), nullable=False, comment='手机号')
    userEmail = Column(String(128), comment='电子邮箱')
    userAddress = Column(String(128), comment='住址')
    departmentId = Column(BigInteger, comment='所属部门 id')
    userStatus = Column(TINYINT, nullable=False, server_default=text("'2'"), comment='员工状态')
    createTime = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"), comment='创建时间')
    updateTime = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"), comment='更新时间')
    isDelete = Column(TINYINT, nullable=False, server_default=text("'0'"), comment='是否删除')

class UserPersonalItem(BaseModel):
    userPersonalId: str
    userAccountId: str
    userName: str
    userGender: int
    userIdCardNum: str
    userPhoneNumber: str
    userEmail: str
    userAddress: str
    departmentId: int
    userStatus: int
    createTime: str
    updateTime: str
    isDelete: int
    model_config = {
        'from_attributes': True,
    }
    
    @field_validator('createTime', 'updateTime', mode='before') 
    @classmethod 
    def convert_datetime_to_str(cls, v: datetime) -> str: 
        return v.isoformat()
    
    @field_validator("departmentId", mode="before")
    @classmethod
    def convert_department_id_to_int(cls, v: int) -> int:
        if v is None:
            return -1
        return v

